using UnityEngine;
using UnityEngine.UI;
using TMPro;

namespace GameSystems
{
    /// <summary>
    /// 登录UI管理器 - 负责登录界面的动画和交互效果
    /// </summary>
    public class LoginUIManager : MonoBehaviour
    {
        #region Serialized Fields

        [Header("动画配置")]
        [Tooltip("淡入淡出时间")]
        [SerializeField] private float fadeDuration = 0.3f;

        [Tooltip("缩放动画时间")]
        [SerializeField] private float scaleDuration = 0.2f;

        [Tooltip("按钮点击缩放效果")]
        [SerializeField] private float buttonClickScale = 0.95f;

        [Header("UI元素")]
        [Tooltip("登录面板")]
        [SerializeField] private RectTransform loginPanel;

        [Tooltip("标题文本")]
        [SerializeField] private TextMeshProUGUI titleText;

        [Tooltip("版本信息文本")]
        [SerializeField] private TextMeshProUGUI versionText;

        [Header("视觉效果")]
        [Tooltip("背景粒子系统")]
        [SerializeField] private ParticleSystem backgroundParticles;

        [Tooltip("输入框高亮颜色")]
        [SerializeField] private Color highlightColor = Color.cyan;

        [Tooltip("输入框正常颜色")]
        [SerializeField] private Color normalColor = Color.white;

        #endregion

        #region Private Fields

        private CanvasGroup canvasGroup;
        private Vector3 originalButtonScale;
        private Color originalInputFieldColor;

        #endregion

        #region Unity Lifecycle

        /// <summary>
        /// 初始化组件
        /// </summary>
        private void Awake()
        {
            InitializeComponents();
        }

        /// <summary>
        /// 启动时初始化
        /// </summary>
        private void Start()
        {
            SetupInitialUI();
            StartBackgroundEffects();
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// 显示登录界面
        /// </summary>
        public void ShowLoginScreen()
        {
            StartCoroutine(FadeInCoroutine());
        }

        /// <summary>
        /// 隐藏登录界面
        /// </summary>
        public void HideLoginScreen()
        {
            StartCoroutine(FadeOutCoroutine());
        }

        /// <summary>
        /// 播放按钮点击动画
        /// </summary>
        /// <param name="button">目标按钮</param>
        public void PlayButtonClickAnimation(Button button)
        {
            if (button != null)
            {
                StartCoroutine(ButtonClickAnimationCoroutine(button));
            }
        }

        /// <summary>
        /// 高亮输入框
        /// </summary>
        /// <param name="inputField">目标输入框</param>
        public void HighlightInputField(TMP_InputField inputField)
        {
            if (inputField != null)
            {
                Image backgroundImage = inputField.GetComponent<Image>();
                if (backgroundImage != null)
                {
                    backgroundImage.color = highlightColor;
                }
            }
        }

        /// <summary>
        /// 恢复输入框正常颜色
        /// </summary>
        /// <param name="inputField">目标输入框</param>
        public void UnhighlightInputField(TMP_InputField inputField)
        {
            if (inputField != null)
            {
                Image backgroundImage = inputField.GetComponent<Image>();
                if (backgroundImage != null)
                {
                    backgroundImage.color = normalColor;
                }
            }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// 初始化组件
        /// </summary>
        private void InitializeComponents()
        {
            canvasGroup = GetComponent<CanvasGroup>();

            if (canvasGroup == null)
            {
                canvasGroup = gameObject.AddComponent<CanvasGroup>();
            }

            if (loginPanel != null)
            {
                loginPanel.localScale = Vector3.one;
            }
        }

        /// <summary>
        /// 设置初始UI状态
        /// </summary>
        private void SetupInitialUI()
        {
            // 设置版本信息
            if (versionText != null)
            {
                versionText.text = $"版本 {Application.version}";
            }

            // 初始透明度为0
            if (canvasGroup != null)
            {
                canvasGroup.alpha = 0f;
            }
        }

        /// <summary>
        /// 启动背景效果
        /// </summary>
        private void StartBackgroundEffects()
        {
            if (backgroundParticles != null && !backgroundParticles.isPlaying)
            {
                backgroundParticles.Play();
            }
        }

        #endregion

        #region Coroutines

        /// <summary>
        /// 淡入动画协程
        /// </summary>
        /// <returns></returns>
        private System.Collections.IEnumerator FadeInCoroutine()
        {
            float elapsedTime = 0f;

            if (canvasGroup != null)
            {
                while (elapsedTime < fadeDuration)
                {
                    elapsedTime += Time.deltaTime;
                    float alpha = Mathf.Clamp01(elapsedTime / fadeDuration);
                    canvasGroup.alpha = alpha;
                    yield return null;
                }

                canvasGroup.alpha = 1f;
            }

            // 添加缩放效果
            if (loginPanel != null)
            {
                Vector3 targetScale = Vector3.one;
                Vector3 startScale = Vector3.one * 0.8f;

                elapsedTime = 0f;
                while (elapsedTime < scaleDuration)
                {
                    elapsedTime += Time.deltaTime;
                    float progress = elapsedTime / scaleDuration;
                    loginPanel.localScale = Vector3.Lerp(startScale, targetScale, progress);
                    yield return null;
                }

                loginPanel.localScale = targetScale;
            }
        }

        /// <summary>
        /// 淡出动画协程
        /// </summary>
        /// <returns></returns>
        private System.Collections.IEnumerator FadeOutCoroutine()
        {
            float elapsedTime = 0f;

            if (canvasGroup != null)
            {
                while (elapsedTime < fadeDuration)
                {
                    elapsedTime += Time.deltaTime;
                    float alpha = Mathf.Clamp01(1f - (elapsedTime / fadeDuration));
                    canvasGroup.alpha = alpha;
                    yield return null;
                }

                canvasGroup.alpha = 0f;
            }
        }

        /// <summary>
        /// 按钮点击动画协程
        /// </summary>
        /// <param name="button">目标按钮</param>
        /// <returns></returns>
        private System.Collections.IEnumerator ButtonClickAnimationCoroutine(Button button)
        {
            Vector3 originalScale = button.transform.localScale;
            Vector3 targetScale = originalScale * buttonClickScale;

            // 缩小
            float elapsedTime = 0f;
            while (elapsedTime < scaleDuration * 0.5f)
            {
                elapsedTime += Time.deltaTime;
                float progress = elapsedTime / (scaleDuration * 0.5f);
                button.transform.localScale = Vector3.Lerp(originalScale, targetScale, progress);
                yield return null;
            }

            // 恢复
            elapsedTime = 0f;
            while (elapsedTime < scaleDuration * 0.5f)
            {
                elapsedTime += Time.deltaTime;
                float progress = elapsedTime / (scaleDuration * 0.5f);
                button.transform.localScale = Vector3.Lerp(targetScale, originalScale, progress);
                yield return null;
            }

            button.transform.localScale = originalScale;
        }

        #endregion
    }
}
